# -*- coding: utf-8 -*-

# 鼠标示例

import sys, pygame
from pygame.locals import *

reload(sys)
sys.setdefaultencoding('utf-8')

def print_text(font, x, y, text, color=(255,255,255), shadow=False):
    if shadow:
        imgText = font.render(text, True, (0, 0, 0))
        screen.blit(imgText, (x - 2, y - 2))
    image = font.render(text, True, color)
    screen.blit(image, (x, y))

white = 255,255,255
purple = 255,0,255
yellow = 255,255,0
green = 0,255,0
red = 255,0,0

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("鼠标示例")
font1 = pygame.font.Font("mini.ttf", 20)

mouse_x = mouse_y = 0
move_x = move_y = 0
mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouse_x,mouse_y = event.pos
            move_x,move_y = event.rel
        elif event.type == MOUSEBUTTONDOWN:
            mouse_down = event.button
            mouse_down_x,mouse_down_y = event.pos
        elif event.type == MOUSEBUTTONUP:
            mouse_up = event.button
            mouse_up_x,mouse_up_y = event.pos

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    screen.fill((0, 100, 0))

    print_text(font1, 0, 0, u"鼠标事件")
    print_text(font1, 0, 20, u"鼠标位置："+str(mouse_x)+","+str(mouse_y))
    print_text(font1, 0, 40, u"鼠标相对位置："+str(move_x)+","+str(move_y))

    print_text(font1, 0, 60, u"鼠标按下：" +str(mouse_down) + " at "+ str(mouse_down_x) + "," + str(mouse_down_y))
    print_text(font1, 0, 80, u"鼠标松开：" +str(mouse_up) + " at "+ str(mouse_up_x) + "," + str(mouse_up_y))

    print_text(font1, 0, 160, u"鼠标滚轮")
    x,y = pygame.mouse.get_pos()
    print_text(font1, 0, 180, u"鼠标位置："+str(x)+","+str(y))

    b1,b2,b3 = pygame.mouse.get_pressed()
    print_text(font1, 0, 200, u"鼠标按键：" + str(b1) + "," + str(b2)+ "," + str(b3))

    pygame.display.update()


